package com.zendesk.maxwell.support;

import com.alibaba.fastjson.JSON;
import com.zendesk.maxwell.replication.BinlogConnectorEvent;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: miaoguoxin
 * @Date: 2021/12/14 17:45
 * @Description:
 */
public class Test2 {

	private static ExecutorService service = Executors.newFixedThreadPool(2, r -> {
		Thread t = new Thread(Thread.currentThread().getThreadGroup(), r, "t", 0);
		t.setDaemon(true);
		if (t.getPriority() != Thread.NORM_PRIORITY) {
			t.setPriority(Thread.NORM_PRIORITY);
		}
		return t;
	});

	public static void main(String[] args) throws InterruptedException {
		LinkedBlockingDeque<DisEvent> queue = new LinkedBlockingDeque<>(10000000);
		long l = System.currentTimeMillis();
		service.execute(new Runnable() {
			@Override
			public void run() {
				while (true){
					try {
						DisEvent disEvent = queue.poll();
						if (disEvent!=null){
							disEvent.setJsonObj(JSON.parseObject(disEvent.getJson()));
							if (disEvent.getI()==1000000) {
								System.out.println(System.currentTimeMillis()-l);
							}
						}

					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			}
		});
		for (int j = 0; j < 10; j++) {
			for (int i = 0; i <= 1000000; i++) {
				DisEvent disEvent = new DisEvent("{\"test\":123,\"a\":\"ffff\"}", System.currentTimeMillis());
				disEvent.setI(i);
				queue.offer(disEvent);
			}
		}
//		for (int i = 0; i < 10000000; i++) {
//			DisEvent disEvent = queue.poll();
//		}
		Thread.sleep(3000);
	}
}
